package cn.dida.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import cn.dida.model.Logistics;
import cn.dida.model.Pageable;
import cn.dida.util.DateUtils;

/**
 * <p>承运方  数据交互层</p>
 * @author lisha
 * @date 2016-11-09 14:04
 * */
@Repository
public class LogisticsDao extends DaoBase {
 
	/**
	 * 增加承运方
	 * @param logistics 承运方实体类
	 * @date 2016-11-09 14:25
	 * @return int 返回受影响的记录行号
	 * */
	public int insert(Logistics logistics) {
        String sql = "insert into logistics(name, website, c_time, c_admin, u_time, u_admin,sta,company_id) values (:name, :website, :c_time, :c_admin, :u_time, :u_admin, :sta,:companyId)";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", logistics.getName());
        paramMap.put("website", logistics.getWebsite());
        paramMap.put("c_time", DateUtils.STYLE3.format(new Date()));
        paramMap.put("c_admin", logistics.getcAdmin());
        paramMap.put("u_time", DateUtils.STYLE3.format(new Date()));
        paramMap.put("u_admin", logistics.getuAdmin());
        paramMap.put("sta", 1);//默认正常状态
        paramMap.put("companyId", logistics.getCompanyId());
       /* try {
			templateTool.save(logistics);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
        
        
        return namedParameterJdbcTemplate.update(sql, paramMap);
    }
	/**
	 * 修改承运方
	 * @param logistics 承运方实体类
	 * @return int 返回受影响的记录行号
	 * @date 2016-11-09 14:29
	 * */
	public int update(Integer companyId,Logistics logistics) {
        String sql = "update logistics set name=:name, website=:website, u_time=:utime, u_admin=:uadmin where id = :id and company_id =:companyId";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", logistics.getName());
        paramMap.put("website", logistics.getWebsite());
        //paramMap.put("c_time", new Date());
        //paramMap.put("c_admin", logistics.getCadmin());
        paramMap.put("utime", DateUtils.STYLE3.format(new Date()));
        paramMap.put("uadmin", logistics.getuAdmin());
        paramMap.put("id", logistics.getId());
        paramMap.put("companyId", companyId);
        
        return namedParameterJdbcTemplate.update(sql, paramMap);
    }
	/**
	 * 删除承运方
	 * @param id 记录行号
	 * @return int 受影响的记录行数
	 * @date 2016-11-09 14:35
	 * */
	public int delete(int id) {
        String sql = "delete from logistics where id = ?";
        return jdbcTemplate.update(sql, id);
    }
	/**
	 * 承运方列表
	 * @param name 查询条件:承运方公司名称
	 * @param page 
	 * @return Pageable分页结果
	 * @date 2016-11-09 14:47
	 * */
	public Pageable<Logistics> query(Integer companyId,String name,  int page) {
        Object[] args = {};
        StringBuffer sqlQueryResultCount = new StringBuffer("select count(1) from logistics where company_id = ?");
        StringBuffer sqlQueryResult = new StringBuffer("select * from logistics where company_id = ?");

        args = ArrayUtils.add(args, companyId);
        StringBuffer sqlWhere = null;
        if(StringUtils.isNotBlank(name)){
        	sqlWhere = new StringBuffer(" and name like ?");
            args = ArrayUtils.add(args, "%" + name + "%");
        }else{
        	sqlWhere = new StringBuffer(" and 1=1");
        }
        
        
        

        // 查询记录总数
        Integer resultTotal = jdbcTemplate.queryForObject(sqlQueryResultCount.append(sqlWhere).toString(), Integer.class, args);
        // 查询记录
        sqlQueryResult.append(sqlWhere).append("  order by c_time desc limit ?,?");
        args = ArrayUtils.add(args, (page - 1) * getPageSize());
        args = ArrayUtils.add(args, getPageSize());
        List<Logistics> result = jdbcTemplate.query(sqlQueryResult.toString(), args, new BeanPropertyRowMapper<>(Logistics.class));
        return new Pageable<>(page, getPageSize(), resultTotal, result);
    }
	/**
	 * 根据记录行号和承运方名称获取承运方详情
	 * @param name 承运方名称
	 * @param id 承运方记录id
	 * @return Logistics 承运方实体信息
	 * @date 2016-11-10 11:11
	 * */
	public Logistics getLogisticsById(Integer companyId,String name, Integer id) {
        String sql = "select * from logistics where id = ? and company_id = ?";
        try {
        	Logistics logistics = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Logistics>(Logistics.class), id,companyId);
            return logistics;
        } catch (DataAccessException e) {
            return null;
        }
    }
	
	/**
	 * 获取承运方列表，兑奖列表中填写配送信息使用
	 * @param state 承运方状态:（1正常，0否）
	 * @return List<Logistics> 承运方列表信息
	 * @date 2016-11-21 16:16
	 * */
	public List<Logistics> getLogisticsList(Integer state){
		String sql = "select * from logistics where sta = ?";
		List<Logistics> result = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(Logistics.class),state);
		return result;
	}
	
	/**
     * 根据承运方名称 查看承运方是否存在
     * @param companyId 公司名称
     * @param name 承运方名称
     * @return boolean true存在，false不存在
     * @date 2016-12-12 12:01
     * */
    public boolean isExistsActivity(Integer companyId, String name){
    	String sql = "select * from logistics where company_id = ? and name = ? ";
    	List<Logistics> result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Logistics.class),companyId,name);
    	if(result.size() > 0){
    		return true;
    	}else{
    		return false;
    	}
    }
}
